let app = getApp();
Page({
    data: {
       header:{},
       userInfo:{}, 
       title:'',
       content:'',
       topic:[{"id":10,"name":"求职","type":10,"sort":2},{"id":11,"name":"招聘","type":20,"sort":3},{"id":12,"name":"求购","type":30,"sort":4},{"id":17,"name":"租赁","type":80,"sort":9},{"id":19,"name":"办证","type":100,"sort":11}],
       hotTopicType:null,
       changeId:null,
       linshiArr:[],
       "province": "湖北省",
       "city": "请选择",
       item:'武汉'

   },
    turnCity:function(){
      wx.navigateTo({
        url: '/pages/cityChoose/webView?type=4'
      })
    },
   onLoad: function(options) {
    console.log("options.type")
    console.log(options.type)
    if (options.type) {
       this.setData({
       hotTopicType:options.type
        })
        var e={
          target:{
            dataset:{
              code:options.type
            }
          }
        }
        this.getBbs(e);

    }
    

     var address= wx.getStorageSync('address');
        this.setData({
            address:address,
            city:address.city,
            item:address.city,
            province:address.province
      })
    getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header
       })  
       if (!options.type) {
        this.gethotTopic();
       }
       
     })
     
    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成

    },
     onMyEvent:function(province){
      console.log(province.detail)
      this.setData({
        province:province.detail.split(',')[0],
        city:province.detail.split(',')[1]
      })
      
     
    },
    onShow: function() {
     
       var chooseProvince= wx.getStorageSync('chooseProvince4');
       var chooseCity2=wx.getStorageSync('chooseCity4');
     
      
        if (chooseProvince) {
          this.setData({
            city:wx.getStorageSync('chooseCity4')||'全国',
            item:wx.getStorageSync('chooseCity4')||'全国',
            province:wx.getStorageSync('chooseProvince4')||'全国'
          })
        }
    

    },
     //获取热门话题
    gethotTopic:function(){
      var arr=this.data.topic;
      for(var i=0;i<arr.length;i++){
               arr[i].select='nomal';
             }
        arr[0].select='select';
        this.setData({
                topic:arr,
       hotTopicType:arr[0].type
     })
       //  var _self=this;
       //   wx.request({
       //      url: getApp().globalData.baseUrl + '/wx/articles/hotTopic',
       //      method:"GET",
       //      header: this.data.header,
       //      success: function(res) {
       //       console.log(res.data)
       //      if (res.data.code==200) {
       //       for(var i=0;i<res.data.obj.length;i++){
       //          res.data.obj[i].select='nomal';
       //       }
       //       res.data.obj[0].select='select';
       //        _self.setData({
       //          topic:res.data.obj,
       //          hotTopicType:res.data.obj[0].type
       //         })  
                    
       //       }
           
       //   }
       // })
    },
    //切换热门话题
    getBbs:function(e){

       var id=e.target.dataset.code;
       var arr=this.data.topic;
       var name='';
       console.log("*********")
       console.log(id)
       for(var j = 0, length2 = arr.length; j < length2; j++){
           if (id==arr[j].type) {
            arr[j].select='select';
            name=arr[j].name;
           }else{
            arr[j].select='nomal';
           }
           
       }
       console.log(123)
       this.setData({
          topic:arr,
          hotTopicType:id
       }) 
    },
    onHide: function() {
        // 生命周期函数--监听页面隐藏

    },
    textTitle:function (e) {
      console.log(e.detail.value)
       this.setData({
         title:e.detail.value
       })
    },
    textContent:function (e) {
       console.log(e.detail.value)
       this.setData({
         content:e.detail.value
       })
    },
    sendSheQu: function(){
        console.log(this.data.title)
        if (this.data.city==''||this.data.city=='请选择'||!this.data.city) {
            wx.showToast({
            title: '请选择城市',
            icon:'none',
            duration: 2000
          })
          return
         }
        if (this.data.title==''||this.data.city=='请选择'||!this.data.city) {
          wx.showToast({
          title: '请填写完整信息',
          icon:'none',
          duration: 2000
        })
         return
        }

       if (this.data.linshiArr.length>6) {
             wx.showToast({
              title: '最多上传6张图片',
              icon:'none',
              duration: 2000
            })
             var  yarr=this.data.linshiArr.slice(0,6);

             this.setData({
               linshiArr:yarr
             })
      
       }
       console.log(this.data.linshiArr)
      
        wx.request({
            url: getApp().globalData.baseUrl + '/wx/articles/save',
            method:"POST",
            data:{
               openid:this.data.userInfo.openid,
               "hotTopicType": this.data.hotTopicType,//主题
                "title": this.data.title,//标题
                "content": this.data.content,//内容
                "province": this.data.province,
                "city":this.data.city,
                "area": "",
                "latitude": "",
                "longitude": "",
                "wxArticlesPic":this.data.linshiArr,
                "status":10
            },
            header: this.data.header,
            success: function(res) {
               console.log(res)
                wx.setStorageSync('chooseProvince4',null);
                wx.setStorageSync('chooseCity4',null);
              wx.switchTab({
                  url: '../../pages/mine/mine'
                })
                                    
            }
            })

    },chooseImage:function () {
      var _self=this;
      wx.chooseImage({
        success (res) { 
          const tempFilePaths = res.tempFilePaths
          console.log(tempFilePaths[0])
          var imgUrl=tempFilePaths[0];
          console.log('##################')
          for (var i = tempFilePaths.length - 1; i >= 0; i--) {
              _self.uploadimg(tempFilePaths[i]);
              console.log(i)
          }
          // wx.uploadFile({
          //    url:getApp().globalData.baseUrl+'/wx/img/url', //仅为示例，非真实的接口地址
          //   //url:getApp().globalData.baseUrl+'/imagesnode/uploadImg',
          //   filePath: tempFilePaths[0],
          //   header: {
          //     'content-type':'multipart/form-data',
          //     'openid':_self.data.userInfo.openid 
          //   },
          //   name: 'filePath',
          //   formData: {
          //     openid:_self.data.userInfo.openid 
          //   },
          //   success (res){
          //    var data = JSON.parse(res.data);
          //    console.log(data.obj);
          //     var arr=_self.data.linshiArr;
          //     var json={url:data.obj}
          //     arr.push(json)
              
          //       _self.setData({
          //           linshiArr:arr
          //       })
          //     //do something
          //   }
          // })
        }
      })
    },
    uploadimg:function (path) {
      var _self=this;
       wx.uploadFile({
             url:getApp().globalData.baseUrl+'/wx/img/url', //仅为示例，非真实的接口地址
            //url:getApp().globalData.baseUrl+'/imagesnode/uploadImg',
            filePath: path,
            header: {
              'content-type':'multipart/form-data',
              'openid':_self.data.userInfo.openid 
            },
            name: 'filePath',
            formData: {
              openid:_self.data.userInfo.openid 
            },
            success (res){
             var data = JSON.parse(res.data);
             console.log(data.obj);
              var arr=_self.data.linshiArr;
              var json={url:data.obj}
              arr.push(json)
              
                _self.setData({
                    linshiArr:arr
                })
              //do something
            }
          })
    },
    deleteImg:function (e) {
       console.log(e.target.dataset.code);
       var index=e.target.dataset.code;
       var arr=this.data.linshiArr;
       arr.splice(index,1);
       this.setData({
        linshiArr:arr
       })
    }

})